#!/usr/bin/ksh
#******************************************************************************
# 
#      filename:  filter
#
#   description:  reads congressmen from website and prints
#
#        author:  Szechy, Jared
#      login id:  cps444-n1.10
#
#         class:  CPS 444
#    instructor:  Perugini
#    assignment:  Homework #5
#
#      assigned:  October 
#           due:  November 4, 2009
#
#******************************************************************************

total=""
{ while read myline;do
   district=0
   url=$(echo $myline | egrep -o 'http://.*')
   page=$(echo $url | lynx -dump -width=200 -)
   state=$(echo "$page" | egrep -m 1 -o '[(][A-Z]{2}[)]' | egrep -o '[A-Z]{2}'\
      | tr '[:upper:]' '[:lower:]')
   district=$(echo "$page" | egrep -m 1 'Current District:' | cut -d ' ' -f 6)
   seniority=$(echo "$page" | egrep -m 1 'Seniority:' | cut -d ' ' -f 5)
   office=$(echo "$page" | egrep -m 1 'Current Office: U.S.' | cut -d " " -f 7)
   party=$(echo "$page" | egrep -m 1 'Party:' | cut -d " " -f 5)

   if [[ $party = "Republican" ]];then
      party="republican"
   elif [[ $party = "Independent" ]];then
      party="independent"
   else
      party="democrat"
   fi

   office=$(echo "$office" | tr '[:upper:]' '[:lower:]')

   if [[ $district = "Delegate" ]];then
      district="at_large"
   elif [[ $district -ne 0 ]];then
      district="district_$district"
   else
      district="district_at_large"
   fi

   if [[ $seniority ]];then
      district=$(echo $seniority | tr '[:upper:]' '[:lower:]')
   fi

   total="$total$state $office $party $district $url~"
   
done } < urls.txt
echo $total | sed 's/~/\n/g' | sort -V -
return 0
